<template>
	<view>
		<view style=" z-index: 1000;background-color: white; width: 100%;"></view>
		<view class="header-max">
			<view class="header-max-content">
				<image @tap="back" src="@/static/images/xiangzuo.png" mode="widthFix" class="back"></image>
				<view class="title">{{title}}</view>
				<!-- #ifndef MP-WEIXIN -->
				
				<view style="display: flex;">
					<view class="publish" @tap="public" style="right: 200rpx;" >发布</view>
					<view class="publish" @click="publicList(1)" style="" v-if="fabu==1">我的发布</view>
					<view class="publish" @click="publicList(0)" style="" v-if="fabu==0">全部发布</view>
				</view>
				<!-- #endif -->
			</view>
		</view>
		<view style="height: 190rpx;"></view>
		<view class="header-search">
			<view class="input">
				<image src="@/static/images/sousuo1.png" class="search-icon" mode="widthFix"></image>
				<input class="search-text" type="text" placeholder="请输入关键字" v-model="keyword">
				<view class="search-btn" @tap="getSearchRes">搜索</view>
			</view>
		</view>
		<view style="height: 20rpx;"></view>

		<view class="item-data">
			<view class="data"  v-for="(item,index) in lists" :key="index">
				<image :src="item.pic" class="data-image" mode="widthFix" @tap="exchangeDetail(item.id)"></image>
				<view class="location" @tap="exchangeDetail(item.id)">
					<image src="@/static/images/weizhifff.png" class="location-img" mode="widthFix"></image>
					<view class="location-text">{{item.location}}</view>
				</view>
				<view class="" style="padding: 10rpx;" @tap="exchangeDetail(item.id)">
					<view class="data-title">{{item.name}}</view>
					<view class="solid">
						<view class="solid-data">{{item.degree}}</view>
					</view>
					<view class="price">
						<view class="price-item">想要换：</view>
						<view class="price-box">{{item.target}}</view>
					</view>
				</view>
				<view class="" v-if="fabu==0"  @click="del(item.id)" style="background-color: rgba(0, 0, 0, 0.1);text-align: center;padding: 10rpx 0;">
					删除
				</view>
			</view>
		</view>
		<view style="height: 30rpx;"></view>
		<uni-load-more :status="loadingType"></uni-load-more>
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	export default {
		components: {
			uniLoadMore
		},
		data() {
			return {
				title: '互换中心',
				keyword: '',
				page: 1,
				lists: [],
				loadingType: 'more',
				fabu:1,
			}
		},

		onLoad: function() {

			uni.showLoading({
				title: '加载中...'
			});

			this.loadData();
			setTimeout(() => {
				this.isLoaded = true;
				uni.hideLoading();
			}, 600)

		},

		methods: {
			del(e){
				console.log(e)
				this.delapi(e)
			},
			async delapi(id){
				var data={
					token:uni.getStorageSync('token'),
					id:id,
				}
				if (uni.getStorageSync('userInfo')) {
					data.uid = uni.getStorageSync('userInfo').uid
				}
				let res = await this.$myRequest({
					url: '/exchangeListDel',
					data,
					methods: 'post'
				})
				if(res.data.status){
					this.lists=[]
					this.loadData(1);
				}
			},
			back() {
				uni.navigateTo({
					url: '/pages7/pages/index/index'
				})
			},
			publicList(e){
				console.log(e)
				this.fabu=e?0:1;
				this.lists=[]
				this.loadData(1);
			},
			public() {
				let url = '/pages7/pages/nimble/exchange_publish';

				uni.navigateTo({
					url: url
				})
			},

			async loadData(type = 'add') {
				if(type!=1){
					//这里是将订单挂载到tab列表下
					if ((this.loadingType === 'loading' || this.loadingType === 'noMore') && type == 'add') {
						//防止重复加载
						return;
					}
				}

				this.loadingType = 'loading';

				let dataObj = {
					page: this.page,
					keyword: this.keyword,
				}
				if(this.fabu==0){//我的发布
					dataObj.token=uni.getStorageSync('token')
					if (uni.getStorageSync('userInfo')) {
						dataObj.uid = uni.getStorageSync('userInfo').uid
					}
				}
				let url = '/exchange_list';

				let ldata = await this.$myRequest({
					url: url,
					data: dataObj,
					methods: 'post'
				})

				let ndata = ldata.data.data.list;
				if (ndata.length > 0) {
					this.lists = [...this.lists, ...ndata];
					let count = ldata.data.data.totalList;
					if (ndata.length >= count) {
						this.page = this.page + 1;
						this.loadingType = 'more';
					} else {
						this.loadingType = 'noMore';
					}
				} else {
					this.loadingType = 'noMore';
				}
			},

			getSearchRes() {
				this.lists = [];
				this.page = 1;
				this.loadingType = 'more';

				this.loadData('refresh')
			},

			exchangeDetail(id) {
				uni.navigateTo({
					url: '/pages7/pages/nimble/exchange_detail?id=' + id
				})
			},
		}
	}
</script>

<style lang="scss">
	.header-max {
		display: flex;
		align-items: center;
		background-color: white;
		position: fixed;
		width: 100%;
		height: 180rpx;
		z-index: 1000;

		.header-max-content {
			width: 100%;
			display: flex;
			justify-content: space-between;
			position: absolute;
			top: 50px;
			padding: 0 30upx;

			.back {
				width: 17rpx;
				height: 30rpx;
				left: 30rpx;
				position: absolute;
			}

			.title {
				margin-left: 40%;
				letter-spacing: 2rpx;
				font-size: 30rpx
			}
		}
	}

	.publish {
		position: absolute;
		right: 30upx;
		font-size: 30upx;
		letter-spacing: 4upx;
		color: white;
		background-color: $base-color;
		border-radius: 50upx;
		padding: 10upx;
	}

	.header-search {
		.input {
			display: flex;
			background-color: white;
			width: 710rpx;
			height: 80rpx;
			padding: 10rpx;
			border: 3rpx solid #f3511a;
			border-radius: 100rpx;
			align-items: center;
			margin: 0rpx auto;
			// margin-bottom: 25rpx;

			.search-text {
				flex: 1;
				color: #6d6e6e;
				font-size: 28rpx;
			}

			.search-btn {
				font-size: 30rpx;
				background-color: #f3511a;
				color: white;
				width: 110rpx;
				letter-spacing: 2rpx;
				height: 57rpx;
				line-height: 57rpx;
				text-align: center;
				border-radius: 50rpx;
			}

			.search-icon {
				width: 25rpx;
				height: 25rpx;
				margin: 0 10rpx;
			}
		}

	}

	.center {
		display: grid;
		grid-template-columns: repeat(4, 195rpx);
		grid-gap: 30rpx 0;

		.center-item {
			display: flex;
			// text-align: center;
			align-items: center;
			justify-content: center;

			.center-icon {
				margin-left: 10rpx;
				width: 14rpx;
				height: 8rpx;
			}
		}

	}

	.item-data {
		// text-align: center;
		display: grid;
		grid-template-columns: repeat(2, 340rpx);
		// grid-template-rows: repeat(2, 540rpx);
		grid-gap: 30rpx;
		margin-left: 24rpx;

		// position: relative;
		.location {
			position: absolute;
			// bottom: 10rpx;
			top: 310rpx;
			left: 20rpx;
			display: flex;
			align-items: center;
		}

		.location-text {
			margin-left: 10rpx;
			color: white;
			font-size: 26rpx;
			width: 88%;
			word-break: keep-all;
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
		}

		.location-img {
			width: 23rpx;
			height: 26rpx;
		}

		.data {
			box-shadow: 5rpx 5rpx 15rpx #c4c4c7;
			border-radius: 20rpx;
			position: relative;

			.data-image {
				width: 340rpx;
				height: 360rpx !important;
				border-top-left-radius: 20upx;
				border-top-right-radius: 20upx;
			}

			.data-title {
				font-weight: 600;
				width: 300rpx;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				margin-left: 10rpx;
				font-size: 30rpx;
				margin-bottom: 10rpx;
			}

			.solid {
				display: flex;
				width: 320rpx;
				white-space: nowrap;
				overflow: hidden;

				.solid-data {
					margin: 10rpx;
					background-color: #e1f0fe;
					color: #3b8bfc;
					padding: 10rpx;
					font-size: 24rpx;
					border-radius: 10rpx;
				}
			}

			.price {
				display: flex;
				margin-left: 10rpx;

				.price-item {
					color: #f3511a;
					font-size: 28rpx;
					margin-right: 5rpx;
				}

				.price-box {
					color: #6d6e6e;
					font-size: 26rpx;
					align-self: center;
					// margin-top: 10rpx;
				}
			}
		}

	}
</style>